package com.coocaa.ops.admin.v1.panel.provider.impl;

import com.coocaa.ops.admin.v1.cornericon.biz.impl.CornerIconBizImpl;
import com.coocaa.ops.admin.v1.layout.biz.impl.LayoutInfoBizImpl;
import com.coocaa.ops.admin.v1.panel.biz.impl.*;
import com.coocaa.ops.admin.v1.panel.convert.BlockQueryConverter;
import com.coocaa.ops.admin.v1.panel.convert.PanelQueryConverter;
import com.coocaa.ops.admin.v1.panel.model.PanelInfoModel;
import com.coocaa.ops.admin.v1.panel.model.query.BlockQueryModel;
import com.coocaa.ops.admin.v1.panel.model.query.SubPanelQueryModel;
import com.coocaa.ops.admin.v1.panel.provider.IPanelInfoQueryProvider;
import com.coocaa.ops.admin.v1.plugin.biz.IPluginInfoBiz;
import com.coocaa.ops.admin.v1.poster.biz.impl.GlobalPictureBizImpl;
import com.coocaa.ops.common.core.base.constant.CornerPositionConstants;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.biz.cornericon.entity.GlobalCornerIconEntity;
import com.coocaa.ops.common.core.biz.layout.entity.LayoutInfoEntity;
import com.coocaa.ops.common.core.biz.panel.constant.BlockTypeConstants;
import com.coocaa.ops.common.core.biz.panel.constant.PanelTypeConstants;
import com.coocaa.ops.common.core.biz.panel.constant.SubPanelVersionConstants;
import com.coocaa.ops.common.core.biz.panel.entity.*;
import com.coocaa.ops.common.core.biz.plugin.entity.PluginInfoEntity;
import com.coocaa.ops.common.core.biz.poster.entity.GlobalPictureEntity;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import com.coocaa.ops.common.tool.json.ParseJsonUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author bjh
 * @description 板块信息原始表-查询provider接口实现
 * @date : 2020-06-15 14:40:18
 */
@Service
public class PanelInfoQueryProviderImpl implements IPanelInfoQueryProvider {
    @Autowired
    private PanelInfoBizImpl thisBizImpl;
    @Autowired
    private BlockContentInfoBizImpl blockContentInfoBiz;
    @Autowired
    private GlobalPictureBizImpl globalPictureBiz;
    @Autowired
    private PanelSubPanelRlsBizImpl panelSubPanelRlsBiz;
    @Autowired
    private CornerIconBizImpl cornerIconBiz;
    @Autowired
    private LayoutInfoBizImpl layoutInfoBiz;
    @Autowired
    private IPluginInfoBiz pluginInfoBiz;
    @Autowired
    private BlockExtendInfoBizImpl blockExtendInfoBiz;
    @Autowired
    private BlockPictureRlsBizImpl blockPictureRlsBiz;

    /**
     * 获取详情信息
     *
     * @param id
     * @param version
     * @return
     */
    @Override
    public PanelInfoEntity getDetailInfo(Integer id, Integer version) {
        PanelInfoEntity entity = this.getEntityByIdAndVersion(id, version);
        LayoutInfoEntity layoutInfoEntity = layoutInfoBiz.queryById(entity.getLayoutId());
        entity.setLayoutInfo(layoutInfoEntity);
        if (PanelTypeConstants.isNormal(entity.getPanelType())) {
            return queryNormalPanel(entity);
        } else if (PanelTypeConstants.isGroup(entity.getPanelType())) {
            return queryGroupPanel(entity);
        } else if (PanelTypeConstants.isPlugin(entity.getPanelType())) {
            return queryPluginPanel(entity);
        }
        return entity;
    }

    /**
     * 分页数据-菜单自己使用
     *
     * @param model
     * @return
     */
    @Override
    public PageResultModel<PanelInfoEntity> pageList(PanelInfoModel model) {
        Map<String, Object> params = PanelQueryConverter.getQueryMap(model);
        PageResultModel<PanelInfoEntity> pageResult = thisBizImpl.getCommonPageList(params, true);
        this.handleQueryRlsForPageData(pageResult.getResultList());
        return pageResult;
    }

    /**
     * 分页数据-其他处选择
     *
     * @param model
     * @return
     */
    @Override
    public PageResultModel<PanelInfoEntity> dataList(PanelInfoModel model) {
        Map<String, Object> params = PanelQueryConverter.getQueryMap(model);
        PageResultModel<PanelInfoEntity> pageResult = thisBizImpl.getCommonPageList(params, false);
        this.handleQueryRlsForPageData(pageResult.getResultList());
        return pageResult;

    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    /**
     * 获取基础信息-根据ID和版本
     *
     * @param id
     * @param version
     * @return
     */
    private PanelInfoEntity getEntityByIdAndVersion(Integer id, Integer version) {
        return thisBizImpl.getEntityByIdAndVersion(id, version);

    }

    private PanelInfoEntity queryNormalPanel(PanelInfoEntity entity) {
        BlockQueryModel blockQuery = new BlockQueryModel(entity.getId(), entity.getCurrentVersion(), 0, ResourceTypeEnum.PANEL.getValue());
        List<VirtuallyBlockInfo> virtuallyBlockInfoList = this.queryVirtuallyBlockList(blockQuery, entity.getHasDmp());
        entity.setBlockList(virtuallyBlockInfoList);
        return entity;
    }

    private List<VirtuallyBlockInfo> queryVirtuallyBlockList(BlockQueryModel blockQuery, Integer hasDmp) {
        List<BlockContentConfigInfo> commonBlockList = blockContentInfoBiz.getMapper().queryCommonBlockList(blockQuery);
        this.handleSetAll(commonBlockList);
        List<BlockContentConfigInfo> dmpBlockList = null;
        if (Objects.equals(1, hasDmp)) {
            dmpBlockList = blockContentInfoBiz.getMapper().queryDmpBlockList(blockQuery);
            this.handleSetAll(dmpBlockList);
        }
        List<VirtuallyBlockInfo> virtuallyBlockInfoList = BlockQueryConverter.convertVirtuallyBlockList(commonBlockList, dmpBlockList);
        return virtuallyBlockInfoList;
    }

    private PanelInfoEntity queryGroupPanel(PanelInfoEntity entity) {
        SubPanelQueryModel model = new SubPanelQueryModel(entity.getId(), entity.getCurrentVersion());
        List<SubPanelConfigInfo> subPanelConfigList = panelSubPanelRlsBiz.getMapper().queryRlsSubPanelList(model);
        for (SubPanelConfigInfo subPanelConfigInfo : subPanelConfigList) {
            BlockQueryModel blockQuery = new BlockQueryModel(subPanelConfigInfo.getId(), SubPanelVersionConstants.SUB_PANEL_VERSION, 1, ResourceTypeEnum.SUB_PANEL.getValue());
            List<VirtuallyBlockInfo> virtuallyBlockInfoList = this.queryVirtuallyBlockList(blockQuery, entity.getHasDmp());
            subPanelConfigInfo.setBlockList(virtuallyBlockInfoList);
        }
        entity.setSubPanelList(subPanelConfigList);
        return entity;
    }

    private PanelInfoEntity queryPluginPanel(PanelInfoEntity panel) {
        if (panel.getPluginSelfId() == null) {
            return panel;
        } else {
            panel.setPluginInfo(pluginInfoBiz.queryById(panel.getPluginSelfId()));
            return panel;
        }
    }


    /**
     * 处理设置信息0全部
     *
     * @param blockContentConfigInfoList
     */
    private void handleSetAll(List<BlockContentConfigInfo> blockContentConfigInfoList) {
        if (CollectionUtils.isEmpty(blockContentConfigInfoList)) {
            return;
        }
        for (BlockContentConfigInfo blockContentConfigInfo : blockContentConfigInfoList) {
            //设置信息-每个推荐位内容
            this.handleSetInfoForEachBlockContentConfigInfo(blockContentConfigInfo);
        }
    }

    /**
     * 设置信息-每个推荐位内容
     *
     * @param blockContentConfigInfo
     */
    private void handleSetInfoForEachBlockContentConfigInfo(BlockContentConfigInfo blockContentConfigInfo) {
        //设置角标信息
        handleSetCornerList(blockContentConfigInfo);
        //设置海报信息
        this.handleSetPoster(blockContentConfigInfo);
        //设置版本
        this.handleSetVersionCode(blockContentConfigInfo);
        //设置插件
        this.handlePluginInfo(blockContentConfigInfo);
        //处理设置推荐位扩展信息
        this.handleSetBlockExtendInfo(blockContentConfigInfo);
        //设置 图片类型，2-替补图，3-背景图,4-异形焦点图,5-高清前景图,6-高清背景图
        List<BlockPictureRlsEntity> blockPictureRlsEntityList = blockPictureRlsBiz.queryByBlockId(blockContentConfigInfo.getId());
        blockContentConfigInfo.setBlockPictureRlsList(blockPictureRlsEntityList);
    }

    /**
     * 设置版本
     *
     * @param blockContentConfigInfo
     */
    private void handleSetVersionCode(BlockContentConfigInfo blockContentConfigInfo) {
        String versionCode = StringUtils.isBlank(blockContentConfigInfo.getOnclick()) ? null :
                ParseJsonUtil.parseStringValue(blockContentConfigInfo.getOnclick(), "versioncode");
        blockContentConfigInfo.setVersionCode(versionCode);
    }

    /**
     * 设置海报信息
     *
     * @param blockContentConfigInfo
     */
    private void handleSetPoster(BlockContentConfigInfo blockContentConfigInfo) {
        GlobalPictureEntity globalPictureEntity = globalPictureBiz.queryById(blockContentConfigInfo.getPosterId());
        blockContentConfigInfo.setGlobalPicture(globalPictureEntity);
        if (null != globalPictureEntity) {
            blockContentConfigInfo.setPictureUrl(globalPictureEntity.getPictureUrl());
        }
    }

    /**
     * 处理设置角标信息
     *
     * @param contentConfigInfoEntity
     * @see CornerPositionConstants
     */
    private void handleSetCornerList(BlockContentConfigInfo contentConfigInfoEntity) {
        List<GlobalCornerIconEntity> cornerIconEntityList = InstanceUtil.newArrayList();
        List<Integer> cornerIdList = contentConfigInfoEntity.getCornerIconIdList();
        if (CollectionUtils.isNotEmpty(cornerIdList)) {
            //左上角
            handleGetCornerIconEntity(cornerIdList, CornerPositionConstants.LEFT_TOP, cornerIconEntityList);
            //右上角
            handleGetCornerIconEntity(cornerIdList, CornerPositionConstants.RIGHT_TOP, cornerIconEntityList);
            //右下角
            handleGetCornerIconEntity(cornerIdList, CornerPositionConstants.RIGHT_BOTTOM, cornerIconEntityList);
            //左下角
            handleGetCornerIconEntity(cornerIdList, CornerPositionConstants.LEFT_BOTTOM, cornerIconEntityList);
        }
        contentConfigInfoEntity.setCornerList(cornerIconEntityList);
    }

    /**
     * 处理角标信息设置
     *
     * @param cornerIdList
     * @param position
     * @param cornerIconEntityList
     */
    private void handleGetCornerIconEntity(List<Integer> cornerIdList, int position, List<GlobalCornerIconEntity> cornerIconEntityList) {
        Integer size = cornerIdList.size();
        if (position > size) {
            return;
        }
        Integer cornerId = cornerIdList.get(position);
        GlobalCornerIconEntity globalCornerIconEntity = null;
        if (cornerId != null && cornerId != 0) {
            globalCornerIconEntity = cornerIconBiz.queryById(cornerId);
            if (null != globalCornerIconEntity) {
                globalCornerIconEntity.setPosition(position);
                cornerIconEntityList.add(globalCornerIconEntity);
            }
        }
    }

    private void handleQueryRlsForPageData(List<PanelInfoEntity> panelInfoEntityList) {
        for (PanelInfoEntity entity : panelInfoEntityList) {
            LayoutInfoEntity layoutInfoEntity = layoutInfoBiz.queryById(entity.getLayoutId());
            entity.setLayoutInfo(layoutInfoEntity);
        }
    }

    private void handlePluginInfo(BlockContentConfigInfo blockEntity) {
        if (BlockTypeConstants.PLUGIN.equals(blockEntity.getBlockType())) {
            PluginInfoEntity pluginEntity = pluginInfoBiz.queryById(blockEntity.getPluginSelfId());
            blockEntity.setPluginInfo(pluginEntity);
        }
    }

    /**
     * 处理设置推荐位扩展信息
     */
    private void handleSetBlockExtendInfo(BlockContentConfigInfo blockEntity) {
        if (BlockTypeConstants.NORMAL.equals(blockEntity.getBlockType())) {
            BlockExtendInfoEntity blockExtendInfoEntity = blockExtendInfoBiz.queryById(blockEntity.getId());
            if (null != blockExtendInfoEntity) {
                blockEntity.setIconTextPositionsJsonStr(blockExtendInfoEntity.getIconTextPositionsJsonStr());
                blockEntity.setBgColor(blockExtendInfoEntity.getBgColor());
            }
        }
    }

}
